githug 通关攻略

记录Githug通关过程。

githug是一个git的命令行游戏。

levels:

#1: init
#2: config
#3: add
#4: commit
#5: clone
#6: clone_to_folder
#7: ignore
#8: include
#9: status
#10: number_of_files_committed
#11: rm
#12: rm_cached
#13: stash
#14: rename
#15: restructure
#16: log
#17: tag
#18: push_tags
#19: commit_amend
#20: commit_in_future
#21: reset
#22: reset_soft
#23: checkout_file
#24: remote
#25: remote_url
#26: pull
#27: remote_add
#28: push
#29: diff
#30: blame
#31: branch
#32: checkout
#33: checkout_tag
#34: checkout_tag_over_branch
#35: branch_at
#36: delete_branch
#37: push_branch
#38: merge
#39: fetch
#40: rebase
#41: repack
#42: cherry-pick
#43: grep
#44: rename_commit
#45: squash
#46: merge_squash
#47: reorder
#48: bisect
#49: stage_lines
#50: find_old_branch
#51: revert
#52: restore
#53: conflict
#54: submodule

第1关

1
2
3
4
5
Name: init
Level: 1
Difficulty: *

A new directory, `git_hug`, has been created; initialize an empty repository in it.
1
2
cd git_hug
git init

第2关

1
2
3
4
5
Name: config
Level: 2
Difficulty: *

Set up your git name and email, this is important so that your commits can be identified.
1
2
3
git config user.name "mark"
git config user.email "shuyumark@gmail.com"
git config --list

第3关

1
2
3
4
5
6
Name: add
Level: 3
Difficulty: *

There is a file in your folder called `README`, you should add it to your staging area
Note: You start each level with a new repo. Don't look for files from the previous one.

1
git add README

第4关

1
2
3
4
5
Name: commit
Level: 4
Difficulty: *

The `README` file has been added to your staging area, now commit it.
1
git commit -m 'add README'

第5关

1
2
3
4
5
Name: clone
Level: 5
Difficulty: *

Clone the repository at https://github.com/Gazler/cloneme.
1
git clone https://github.com/Gazler/cloneme

第6关

1
2
3
4
5
Name: clone_to_folder
Level: 6
Difficulty: *

Clone the repository at https://github.com/Gazler/cloneme to `my_cloned_repo`.
1
git clone https://github.com/Gazler/cloneme my_cloned_repo

第7关

1
2
3
4
5
Name: ignore
Level: 7
Difficulty: **

The text editor 'vim' creates files ending in `.swp` (swap files) for all files that are currently open. We don't want them creeping into the repository. Make this repository ignore `.swp` files.
1
2
touch .gitignore
echo '*.swp' > .gitignore

第8关

1
2
3
4
5
Name: include
Level: 8
Difficulty: **

Notice a few files with the '.a' extension. We want git to ignore all but the 'lib.a' file.
1
2
echo '*.a' >> .gitignore 
echo '!lib.a' >> .gitignore

第9关

1
2
3
4
5
Name: status
Level: 9
Difficulty: *

There are some files in this repository, one of the files is untracked, which file is it?
1
git status

第10关

1
2
3
4
5
Name: number_of_files_committed
Level: 10
Difficulty: *

There are some files in this repository, how many of the files will be committed?
1
git status

第11关

1
2
3
4
5
Name: rm
Level: 11
Difficulty: **

A file has been removed from the working tree, however the file was not removed from the repository. Find out what this file was and remove it.
1
2
git status
git rm deleteme.rb

第12关

1
2
3
4
5
Name: rm_cached
Level: 12
Difficulty: **

A file has accidentally been added to your staging area, find out which file and remove it from the staging area. *NOTE* Do not remove the file from the file system, only from git.
1
git rm --cached deleteme.rb

第13关

1
2
3
4
5
Name: stash
Level: 13
Difficulty: **

You've made some changes and want to work on them later. You should save them, but don't commit them.
1
git stash

第14关

1
2
3
4
5
Name: rename
Level: 14
Difficulty: ***

We have a file called `oldfile.txt`. We want to rename it to `newfile.txt` and stage this change.
1
git mv oldfile.txt newfile.txt

第15关

1
2
3
4
5
Name: restructure
Level: 15
Difficulty: ***

You added some files to your repository, but now realize that your project needs to be restructured. Make a new folder named `src` and using Git move all of the .html files into this folder.
1
2
mkdir src
git mv *.html src

第16关

1
2
3
4
5
Name: log
Level: 16
Difficulty: **

You will be asked for the hash of most recent commit. You will need to investigate the logs of the repository for this.
1
git log

第17关

1
2
3
4
5
Name: tag
Level: 17
Difficulty: **

We have a git repo and we want to tag the current commit with `new_tag`.
1
git tag new_tag HEAD // 默认是HEAD,可以省略

第18关

1
2
3
4
5
6
7
8
9
Name: push_tags
Level: 18
Difficulty: **

There are tags in the repository that aren't pushed into remote repository. Push them now.


From /var/folders/3t/kh85db8d1c39qhjhx_h0nxx00000gn/T/d20160215-3639-161bq0y/
* [new branch] master -> origin/master

1
git push origin master

第19关

1
2
3
4
5
Name: commit_amend
Level: 19
Difficulty: **

The `README` file has been committed, but it looks like the file `forgotten_file.rb` was missing from the commit. Add the file and amend your previous commit to include it.
1
2
git add forgotten_file.rb
git commit --amend

第20关

1
2
3
4
5
Name: commit_in_future
Level: 20
Difficulty: **

Commit your changes with the future date (e.g. tomorrow).
1
2
git commit --help //查看doc
git commit --date=2016.02.16 -m 'add README'

第21关

1
2
3
4
5
Name: reset
Level: 21
Difficulty: **

There are two files to be committed. The goal was to add each file as a separate commit, however both were added by accident. Unstage the file `to_commit_second.rb` using the reset command (don't commit anything).
1
git reset HEAD to_commit_second.rb

第22关

1
2
3
4
5
Name: reset_soft
Level: 22
Difficulty: **

You committed too soon. Now you want to undo the last commit, while keeping the index.
1
git reset --soft HEAD^

第23关

1
2
3
4
5
Name: checkout_file
Level: 23
Difficulty: ***

A file has been modified, but you don't want to keep the modification. Checkout the `config.rb` file from the last commit.
1
git checkout -- config.rb

第24关

1
2
3
4
5
Name: remote
Level: 24
Difficulty: **

This project has a remote repository. Identify it.
1
git remote -v

第25关

1
2
3
4
5
Name: remote_url
Level: 25
Difficulty: **

The remote repositories have a url associated to them. Please enter the url of remote_location.
1
git remote -v

第26关

1
2
3
4
5
Name: pull
Level: 26
Difficulty: **

You need to pull changes from your origin repository.
1
git pull origin master

第27关

1
2
3
4
5
Name: remote_add
Level: 27
Difficulty: **

Add a remote repository called `origin` with the url https://github.com/githug/githug
1
git remote add origin https://github.com/githug/githug

第28关

1
2
3
4
5
Name: push
Level: 28
Difficulty: ***

Your local master branch has diverged from the remote origin/master branch. Rebase your commit onto origin/master and push it to remote.
1
2
git pull --rebase
git push origin master

第29关

1
2
3
4
5
Name: diff
Level: 29
Difficulty: **

There have been modifications to the `app.rb` file since your last commit. Find out which line has changed.
1
git diff

第30关

1
2
3
4
5
Name: blame
Level: 30
Difficulty: **

Someone has put a password inside the file `config.rb` find out who it was.
1
git blame config.rb

第31关

1
2
3
4
5
Name: branch
Level: 31
Difficulty: *

You want to work on a piece of code that has the potential to break things, create the branch test_code.
1
git branch test_code

第32关

1
2
3
4
5
Name: checkout
Level: 32
Difficulty: **

Create and switch to a new branch called my_branch. You will need to create a branch like you did in the previous level.
1
git checkout -b my_branch

第33关

1
2
3
4
5
Name: checkout_tag
Level: 33
Difficulty: **

You need to fix a bug in the version 1.2 of your app. Checkout the tag `v1.2`.
1
git checkout v1.2

第34关

1
2
3
4
5
Name: checkout_tag_over_branch
Level: 34
Difficulty: **

You need to fix a bug in the version 1.2 of your app. Checkout the tag `v1.2` (Note: There is also a branch named `v1.2`).

这里有个分支和tag都叫v1.2,想切换到v1.2分支直接git checkout v1.2就可以,想切换到v1.2的tag,为了区别v1.2分支就需要指定tag,git checkout tags/v1.2

第35关

1
2
3
4
5
Name: branch_at
Level: 35
Difficulty: ***

You forgot to branch at the previous commit and made a commit on top of it. Create branch test_branch at the commit before the last.

HEAD^指向当前提交的上一次提交,git branch test_branch HEAD^就在上一个提交处创建了一个test_branch的分支

第36关

1
2
3
4
5
Name: delete_branch
Level: 36
Difficulty: **

You have created too many branches for your project. There is an old branch in your repo called 'delete_me', you should delete it.
1
git branch -d delete_me

第37关

1
2
3
4
5
Name: push_branch
Level: 37
Difficulty: **

You've made some changes to a local branch and want to share it, but aren't yet ready to merge it with the 'master' branch. Push only 'test_branch' to the remote repository
1
git push origin test_branch:test_branch

第38关

1
2
3
4
5
Name: merge
Level: 38
Difficulty: **

We have a file in the branch 'feature'; Let's merge it to the master branch.
1
git merge feature

第39关

1
2
3
4
5
Name: fetch
Level: 39
Difficulty: **

Looks like a new branch was pushed into our remote repository. Get the changes without merging them with the local repository
1
git fetch

第40关

1
2
3
4
5
Name: rebase
Level: 40
Difficulty: **

We are using a git rebase workflow and the feature branch is ready to go into master. Let's rebase the feature branch onto our master branch.

详见rebase

1
2
3
4
git checkout feature 
git rebase master
git checkout master
git merge feature

第41关

1
2
3
4
5
Name: repack
Level: 41
Difficulty: **

Optimise how your repository is packaged ensuring that redundant packs are removed.

详见repack

1
git repack -d

第42关

1
2
3
4
5
Name: cherry-pick
Level: 42
Difficulty: ***

Your new feature isn't worth the time and you're going to delete it. But it has one commit that fills in `README` file, and you want this commit to be on the master as well.

cherry-pick

1
2
3
4
git checkout new-feature 
git log
git checkout master
git cherry-pick ca32a6d

第43关

1
2
3
4
5
Name: grep
Level: 43
Difficulty: **

Your project's deadline approaches, you should evaluate how many TODOs are left in your code
1
git grep TODO

第44关

1
2
3
4
5
Name: rename_commit
Level: 44
Difficulty: ***

Correct the typo in the message of your first (non-root) commit.
1
2
git log
git rebase -i 58885b7

First coommit那行的pick 改为reword,然后保存,之后就可以修改提交信息

第45关

1
2
3
4
5
Name: squash
Level: 45
Difficulty: ****

You have committed several times but would like all those changes to be one commit.
1
2
git log
git rebase -i f733099

将要squash的行的pick 改为squash,然后保存,之后就可以修改提交信息

第46关

1
2
3
4
5
Name: merge_squash
Level: 46
Difficulty: ***

Merge all commits from the long-feature-branch as a single commit.

Hint:Take a look at the --squash option of the merge command. Don’t forget to commit the merge!

1
2
git merge --squash long-feature-branch
git commit -m 'update'

第47关

1
2
3
4
5
Name: reorder
Level: 47
Difficulty: ****

You have committed several times but in the wrong order. Please reorder your commits.

Hint: Take a look the -i flag of the rebase command.

1
git rebase -i HEAD^^

然后调换两个commit的顺序即可。

第48关

1
2
3
4
5
Name: bisect
Level: 48
Difficulty: ***

A bug was introduced somewhere along the way. You know that running `ruby prog.rb 5` should output 15. You can also run `make test`. What are the first 7 chars of the hash of the commit that introduced the bug.

git bisect通过二分查找来帮助查找是哪个commit引入错误。

1
2
3
4
5
6
7
git bisect start // 开始查找
git bisect bad // 标记当前有问题
git bisect good f608824 // 标记第一次提交是没问题的
// 之后git自动会将head指向中间的那个commit
ruby prog.rb 5 // 检测是否有问题
git bisect good // 标记这次提交是没问题的
...

一直重复,就能找到出错的commit。

第49关

1
2
3
4
5
Name: stage_lines
Level: 49
Difficulty: ****

You've made changes within a single file that belong to two different features, but neither of the changes are yet staged. Stage only the changes belonging to the first feature.

Hint: 查看git add的说明。

git add -e feature.rb然后删除不想add的变更。

第50关

1
2
3
4
5
Name: find_old_branch
Level: 50
Difficulty: ****

You have been working on a branch but got distracted by a major issue and forgot the name of it. Switch back to that branch.

使用git reflog来查看是从哪个分支切换过来的。

第51关

1
2
3
4
5
6
Name: revert
Level: 51
Difficulty: ****

You have committed several times but want to undo the middle commit.
All commits have been pushed, so you can't change existing history.

Hint git revert

git revert HEAD^

第52关

1
2
3
4
5
Name: restore
Level: 52
Difficulty: ****

You decided to delete your latest commit by running `git reset --hard HEAD^`. (Not a smart thing to do.) You then change your mind, and want that commit back. Restore the deleted commit.
1
2
git reflog
git reset --hard e601101

第53关

1
2
3
4
5
Name: conflict
Level: 53
Difficulty: ****

You need to merge mybranch into the current branch (master). But there may be some incorrect changes in mybranch which may cause conflicts. Solve any merge-conflicts you come across and finish the merge.
1
2
3
4
git merge mybranch
//打开poem.txt 解决冲突

git add .
git commit

第54关

1
2
3
4
5
Name: submodule
Level: 54
Difficulty: **

You want to include the files from the following repo: `https://github.com/jackmaney/githug-include-me` into a the folder `./githug-include-me`. Do this without cloning the repo or copying the files from the repo into this repo.

Hint: git submodule

1
git submodule add -- https://github.com/jackmaney/githug-include-me ./githug-include-me